using System.Linq;
using MetaObjects.Contracts;
using MetaObjects.Core;
using Microsoft.SharePoint;

namespace MetaObjects.SharePoint
{
	public class SharePointListProvider : MetaEntityProvider<SPList>
	{
		public IMetaFieldProvider<SPField> FieldProvider { get; private set; }

		public SharePointListProvider()
			: this(new SharePointFieldProvider())
		{
		}

		public SharePointListProvider(IMetaFieldProvider<SPField> fieldProvider)
		{
			FieldProvider = fieldProvider;
		}

		protected override IMetaEntity GetMetaEntity(SPList entity)
		{
			var fields = entity.Fields.OfType<SPField>().Select(FieldProvider.FieldFactory);
			var metaEntity = new SharePointList(entity.Title, entity.Title, entity.Items.Add, fields);
			return metaEntity;
		}
	}
}